//
//  XGL_MessageChatInputView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/27.
//

import UIKit

class XGL_MessageChatInputView: UIView {
    
    var inputTextContentView: UIView!
    var inputTextView: PanTextView!
    var moreItem: UIButton!
    var sendItem: UIButton!
    var moreItemsView: XGL_MessageChatInputBarView!
    
    private var textOriginalHeight: CGFloat = 36.0

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.hexColor("#001310").withAlphaComponent(0.5)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XGL_MessageChatInputView{
    @objc private func moreItemAction(sender: UIButton){
        sender.isSelected = !sender.isSelected
        if sender.isSelected{
            self.showMoreItemsView()
        }else{
            self.closeMoreItemsView()
        }
    }
}

extension XGL_MessageChatInputView{
    private func showMoreItemsView(){
        UIView.animate(withDuration: 0.25) {
            self.moreItemsView.isHidden = false
            self.moreItemsView.snp.updateConstraints { make in
                make.height.equalTo(48.0)
            }
        }
    }
    private func closeMoreItemsView(){
        UIView.animate(withDuration: 0.25) {
            self.moreItemsView.snp.updateConstraints { make in
                make.height.equalTo(0)
            }
        } completion: { _ in
            self.moreItemsView.isHidden = true
        }

    }
}

extension XGL_MessageChatInputView{
    
    public func restoreInitialization(){
        self.inputTextView.text = ""
        self.inputTextView.resignFirstResponder()
        self.handleTextHeight(textView: self.inputTextView)
    }
   
    private func handleTextHeight(textView: UITextView) {

        let size = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: CGFloat(MAXFLOAT)))
        let currentHeight = CGFloat(ceilf(Float(size.height))) > self.textOriginalHeight ? CGFloat(ceilf(Float(size.height))) : self.textOriginalHeight
        if currentHeight > self.textOriginalHeight{
            self.inputTextContentView.layerCornerRadius = 4.0
        }else{
            self.inputTextContentView.layerCornerRadius = self.textOriginalHeight / 2.0
        }
        self.inputTextContentView.snp.updateConstraints { make in
            make.height.equalTo(currentHeight)
        }
        UIView.animate(withDuration: 0.25) {
            self.superview!.layoutIfNeeded()
        }
    }
}

extension XGL_MessageChatInputView: UITextViewDelegate{
    func textViewDidChange(_ textView: UITextView) {

        self.handleTextHeight(textView: textView)
        
    }
}

extension XGL_MessageChatInputView{
    private func createXglView(){
        let barContentView = UIView()
        let bottomView = UIView()
        self.inputTextContentView = UIView().then({ make in
            make.backgroundColor = UIColor.black
            make.layerCornerRadius = 18.0
            make.clipsToBounds = true
        })
        self.inputTextView = PanTextView().then({ make in
            make.textColor = UIColor.white
            make.placeholder = "Say something"
            make.placeholderColor = UIColor.hexColor("#A2A2A2")
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.delegate = self
            make.backgroundColor = .clear
        })
        self.moreItem = UIButton().then({ make in
            make.setImage(imageWithName("img_xgl_message_input_open"), for: .normal)
            make.setImage(imageWithName("img_xgl_message_input_close"), for: .selected)
            make.addTarget(self, action: #selector(self.moreItemAction(sender:)), for: .touchUpInside)
        })
        self.sendItem = UIButton().then({ make in
            make.setImage(imageWithName("icon_xgl_input_bar_send"), for: .normal)
        })
        self.moreItemsView = XGL_MessageChatInputBarView().then({ make in
            make.isHidden = true
        })
        self.inputTextContentView.addSubview(self.inputTextView)
        barContentView.addSubviews([self.inputTextContentView,self.moreItem,self.sendItem])
        self.addSubviews([barContentView,self.moreItemsView,bottomView])
        
        self.inputTextView.snp.makeConstraints { make in
            make.center.height.equalToSuperview()
            make.left.equalTo(10.0)
        }
        self.inputTextContentView.snp.makeConstraints { make in
            make.top.equalTo(8.0)
            make.left.equalTo(15.0)
            make.right.equalTo(moreItem.snp.left).offset(-16.0)
            make.height.equalTo(36.0)
        }
        self.moreItem.snp.makeConstraints { make in
            make.top.equalTo(8.0)
            make.right.equalTo(sendItem.snp.left).offset(-16.0)
            make.width.height.equalTo(36.0)
        }
        self.sendItem.snp.makeConstraints { make in
            make.top.equalTo(8.0)
            make.right.equalTo(-15.0)
            make.width.equalTo(76.0)
            make.height.equalTo(36.0)
        }
        self.moreItemsView.snp.makeConstraints { make in
            make.width.centerX.equalToSuperview()
            make.bottom.equalTo(bottomView.snp.top)
            make.height.equalTo(0)
        }
        barContentView.snp.makeConstraints { make in
            make.width.centerX.equalToSuperview()
            make.bottom.equalTo(moreItemsView.snp.top)
            make.bottom.equalTo(self.inputTextView.snp.bottom).offset(6.0)
        }
        bottomView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
//            make.top.equalTo(self.contentView.snp.bottom)
            make.height.equalTo(BottomHeight + 15.0)
        }
        self.snp.makeConstraints { make in
            make.top.equalTo(barContentView.snp.top)
        }
    }
}
